import { _decorator, AudioClip, CCFloat, Component, Label, Node } from 'cc';
import { Bird } from './Bird';
import { MoveBg } from './MoveBg';
import { PipeSpawner } from './PipeSpawner';
import { GameReadyUI } from './UI/GameReadyUI';
import { GameData } from './GameData';
import { GameOverUI } from './UI/GameOverUI';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;
enum GameState {
    Ready,
    Gaming,
    GameOver
}
@ccclass('GameManager')
export class GameManager extends Component {
    private static _inst: GameManager = null;
    public static inst() {
        return this._inst;
    }
    @property(CCFloat)
    moveSpeed: number = 100;
    
    @property(Bird)
    bird: Bird = null;

    @property(MoveBg)
    bgMoving: MoveBg = null;
    @property(MoveBg)
    landMoving: MoveBg = null;
    @property(PipeSpawner)
    pipeSpawner: PipeSpawner = null;
    @property(GameReadyUI)
    gameReadyUI: GameReadyUI = null;
    @property(GameOverUI)
    gameOverUI: GameOverUI = null;
    @property(Node)
    gamingUI: Node = null;
    
    @property(Label)
    scoreLabel: Label = null;

    @property(AudioClip)
    bgAudio: AudioClip = null;
    @property(AudioClip)
    gameOverAudio: AudioClip = null;

    curGS: GameState = GameState.Ready;
    onLoad() {
        GameManager._inst = this;
    }
    protected start () {
        this.transitionToReadyState()
        AudioMgr.inst.play(this.bgAudio, 0.1);
    }
    watchBird(){
        this.bird.node.on('addScore', this.addScore, this);
        this.bird.node.on('overGame', this.transitionToGameOverState, this);
    }
    offWatchBird(){
        this.bird.node.off('addScore', this.addScore, this);
        this.bird.node.off('overGame', this.transitionToGameOverState, this);
    }
    transitionToReadyState(){
        this.curGS = GameState.Ready;

        this.bird.disableControl();
        this.bgMoving.disableMoving();
        this.landMoving.disableMoving();
        this.pipeSpawner.pause()
        this.gamingUI.active = false;

        this.gameOverUI.hide()
        this.gameReadyUI.show()
    }
    transitionToGamingState(){
        this.curGS = GameState.Gaming
        this.bird.enableControl();
        this.bgMoving.enableMoving();
        this.landMoving.enableMoving();
        this.pipeSpawner.generate()
        this.gameReadyUI.hide()
        this.gamingUI.active = true;
        this.watchBird()
    }
    transitionToGameOverState(){
        if(this.curGS === GameState.GameOver) return
        this.curGS = GameState.GameOver;

        this.bird.disableControlNotRGD();
        this.bgMoving.disableMoving();
        this.landMoving.disableMoving();
        this.pipeSpawner.pause()
        this.gamingUI.active = false;

        this.gameOverUI.show(GameData.getScore(), GameData.getBestScore())
        GameData.saveBestScore();
        AudioMgr.inst.stop();
        AudioMgr.inst.playOneShot(this.gameOverAudio);
        this.offWatchBird()
    }

    addScore(count: number=1) {
        GameData.addScore(count);
        this.scoreLabel.string = GameData.getScore().toString();
    }
}

